package com.lw.sbdemo2.common.config;

import com.lw.sbdemo2.common.aop.ParameterAspect;
import com.lw.sbdemo2.common.utils.SpringContextHolder;
import com.lw.sbdemo2.common.web.returnhandle.ResultRequestMappingHandlerAdapter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcRegistrations;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;

import java.nio.charset.Charset;
import java.util.List;

/**
 * <p>标题: 自定义配置类</p>
 * <p>描述: 开启定时器支持、强制使用cglib实现动态粉丝</p>
 * <p>版权: Copyright (c) 2018</p>
 * <p>公司: 智业软件</p>
 *
 * @author LinWang
 * @date 2018-02-25 14:34
 */
@Configuration
@EnableScheduling
//@EnableAspectJAutoProxy(proxyTargetClass = true)
@Slf4j
public class CustomConfig implements WebMvcConfigurer {

    @Value(value = "${info.app.baseUrl}")
    private String baseUrl;

    /**
     * Spring环境变量工具类
     */
    @Bean
    @Lazy(false)
    SpringContextHolder springContextHolder() {
        return new SpringContextHolder();
    }

    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    /**
     * 增加参数校验
     * @return
     */
    @Bean
    public ParameterAspect parameterAspect() {
        return new ParameterAspect();
    }

    @Bean
    public FilterRegistrationBean filterRegist() {
        FilterRegistrationBean frBean = new FilterRegistrationBean();
        frBean.setFilter(new CustomFilter());
        frBean.addUrlPatterns("/*");
        frBean.addInitParameter("encoding", "UTF-8");
        frBean.addInitParameter("baseUrl", baseUrl);
        frBean.setName("sessionFilter");
        return frBean;
    }

    /**
     * 注册自定义Spring拦截器
     * @param registry 拦截器注册表对象
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new CustomInterceptor()).addPathPatterns("/**");
    }

    /**
     * 配置跨域拦截器
     * @param registry 跨域拦截器
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowCredentials(true)
                .allowedHeaders("*")
                .allowedOrigins("*")
                .allowedMethods("*");
    }


    /**
     * 添加自定义消息转换器
     * 自定义消息转化器的第二种方法
     * 默认也会注册utf-8的该转换器
     */
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        StringHttpMessageConverter converter = new StringHttpMessageConverter(Charset.forName("UTF-8"));
        converters.add(converter);
    }

    /**
     * 添加自定义参数解析器
     */
    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
    }

    /**
     * 添加自定义返回值处理器
     */
    @Bean
    public WebMvcRegistrations feignWebRegistrations() {
        return new WebMvcRegistrations() {
            @Override
            public RequestMappingHandlerAdapter getRequestMappingHandlerAdapter() {
                return new ResultRequestMappingHandlerAdapter();
            }
        };
    }
}
